package com.auxgroup.smarthome.app.service;

import com.auxgroup.smarthome.app.entity.SmartElectricityRedis;
import com.auxgroup.smarthome.constant.cache.CachedConsant;
import com.auxgroup.smarthome.redis.config.ObjectRedis;
import com.auxgroup.smarthome.utils.common.LOG;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 智能用电执行规则
 * @Author: laiqiuhua.
 * @Date: 2017/9/20 10:08.
 */
@Service
public class SmartElectricityExecuteService {
    @Autowired
    private ObjectRedis objectRedis;

    /**
     *  每10分钟的期望功率百分比
     * @param ratedPower 额定功率
     * @return 期望功率
     */
    public float calculateExpectedPowerByPerTenMinutes(String mac, int ratedPower) {
        String redisKey = CachedConsant.DEVICE_SMART_ELECTRICITY_PREFIX+mac;
        SmartElectricityRedis smartElecRedis = objectRedis.get(redisKey, SmartElectricityRedis.class);
        if(null == smartElecRedis) return 0f;
        float expectElec = calculateExpectedUseElectricty(mac);
        float power = 6*1000*expectElec/ratedPower;
        LOG.info(this, "第"+smartElecRedis.getN()+"次用户期望功率为"+power);
        return power;
    }

    /**
     * 计算用户的期望用电
     * @param mac
     * @return
     */
    public float calculateExpectedUseElectricty(String mac) {
        String redisKey = CachedConsant.DEVICE_SMART_ELECTRICITY_PREFIX+mac;
        SmartElectricityRedis smartElecRedis = objectRedis.get(redisKey, SmartElectricityRedis.class);
        if(null == smartElecRedis) return 0f;
        float tenMinutesElectricty = smartElecRedis.getSurplusElec()/Float.valueOf(smartElecRedis.getTotal()-smartElecRedis.getN());
        BigDecimal tenMinDecimal = new BigDecimal(tenMinutesElectricty).setScale(3, BigDecimal.ROUND_HALF_UP);
        LOG.info(this, "第"+smartElecRedis.getN()+"次十分钟期望用电值====》"+ tenMinDecimal.floatValue());
        return tenMinDecimal.floatValue();
    }

    /**
     *  智能用电下发功率
     *  根据模式计算：极速模式初始放大5倍，均衡模式保持平衡，标准模式初始放大3倍
     *@param mac
     * @param electricity 十分钟上报电量
     * @param ratedPower 额定功率
     * @return
     */
    public int executeElectricityPower(String mac, float electricity, int ratedPower) {
        StringBuffer eleStrLog = new StringBuffer();
        eleStrLog.append("[智能用电]---")
                .append("mac:")
                .append(mac)
                .append(" electricity:")
                .append(electricity)
                .append(" ratePower:")
                .append(ratedPower);
        String redisKey = CachedConsant.DEVICE_SMART_ELECTRICITY_PREFIX+mac;
        SmartElectricityRedis smartElecRedis = objectRedis.get(redisKey, SmartElectricityRedis.class);
        float tenMinutePower = calculateExpectedPowerByPerTenMinutes(mac, ratedPower);
        int execPower = Float.valueOf(tenMinutePower*smartElecRedis.getScale()*100).intValue();
        eleStrLog.append(" scale:").append(smartElecRedis.getScale());
        // 智能用电功率范围30%< power < 200%
        if(execPower <= 30) execPower = 30;
        if(execPower >= 200) execPower = 200;
        smartElecRedis.excuteCommand(execPower, electricity);
        objectRedis.add(redisKey, smartElecRedis);
        eleStrLog.append(" execPower:").append(execPower).append("%");
        LOG.info(this, eleStrLog.toString());
        return execPower;
    }

}
